package com.cskaoyan.controller.wxcontroller;

import com.cskaoyan.bean.param.WxBaseParam;
import com.cskaoyan.bean.param.WxMyCouponListParam;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.wxvo.WxCouponListVo;
import com.cskaoyan.bean.wxvo.WxCouponReceiveVo;
import com.cskaoyan.bean.wxvo.WxCouponVo;
import com.cskaoyan.service.WxCouponService;
import com.cskaoyan.utils.wx.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("wx/coupon")
public class WxCouponController {

    @Autowired
    WxCouponService wxCouponService;

    @RequestMapping("list")
    public BaseRespVo couponlist(WxBaseParam param) {

        WxCouponListVo wxCouponListVo = wxCouponService.couponList (param);
        return BaseRespVo.ok (wxCouponListVo);
    }

    @RequestMapping("mylist")
    public BaseRespVo couponMylist(WxMyCouponListParam param) {
        Integer userId = UserUtil.getUserId ();
        WxCouponListVo wxCouponListVo = wxCouponService.myList (param, userId);
        return BaseRespVo.ok (wxCouponListVo);
    }

    @RequestMapping("selectlist")
    public BaseRespVo selectList() {
        Integer userId = UserUtil.getUserId ();
        List<WxCouponVo> wxCouponVos = wxCouponService.selectList (userId);
        return BaseRespVo.ok (wxCouponVos);
    }

    @RequestMapping("receive")
    public WxCouponReceiveVo receiveCoupon(@RequestBody Map<String,Integer> map) {
        Integer couponId = map.get ("couponId");
        Integer userId = UserUtil.getUserId ();
        WxCouponReceiveVo wxCouponReceiveVo = wxCouponService.receiveCoupon (couponId, userId);
        return wxCouponReceiveVo;
    }

    @PostMapping("exchange")
    public WxCouponReceiveVo exchangeCoupon(@RequestBody Map<String,String> map) {
        String code = map.get ("code");
        Integer userId = UserUtil.getUserId ();
        WxCouponReceiveVo wxCouponReceiveVo = wxCouponService.exchangeCoupon (code, userId);
        return wxCouponReceiveVo;
    }
}
